﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class AddMatch : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Team team;
    List<Team> teams;
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    MatchType matchType;
    List<MatchType> matchTypes;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                //bindTeam();
                bindType();
                if (Request.QueryString["Id"] != null)
                {
                    fillMatch(Request.QueryString["Id"].ToString());
                }
                bindMatchType();
            }
        }
    }

    private void bindType()
    {
        Series series = new Series(_dataObject);
        List<Series> lstSeries = new List<Series>();
        lstSeries = series.GetAllSeries();
        if (lstSeries.Count > 0)
        {
            drpType.DataSource = lstSeries;
            drpType.DataTextField = "Name";
            drpType.DataValueField = "Id";
            drpType.DataBind();
            drpType.Items.Insert(0, new ListItem("Select Type", "0"));
        }
        else
        {
            drpType.DataSource = null;
            drpType.DataBind();
            drpType.Items.Insert(0, new ListItem("Select Type", "0"));
        }
    }
    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpType.SelectedValue != "0")
            bindTeam(drpType.SelectedValue);
    }

    private void bindTeam(string TypeId)
    {
        var SeriesId = Convert.ToInt32(TypeId);
        team = new Team(_dataObject);
        teams = new List<Team>();
        teams = team.GetTeamsBySeriesId(SeriesId);
        if (teams.Count > 0)
        {
            drpTeam1.DataSource = teams;
            drpTeam1.DataTextField = "TeamName";
            drpTeam1.DataValueField = "Id";
            drpTeam1.DataBind();
            drpTeam1.Items.Insert(0, new ListItem("Select Team", "0"));

            drpTeam2.DataSource = teams;
            drpTeam2.DataTextField = "TeamName";
            drpTeam2.DataValueField = "Id";
            drpTeam2.DataBind();
            drpTeam2.Items.Insert(0, new ListItem("Select Team", "0"));
        }
        else
        {
            drpTeam1.DataSource = teams;            
            drpTeam1.DataBind();
            drpTeam1.Items.Insert(0, new ListItem("Select Team", "0"));

            drpTeam2.DataSource = teams;
            drpTeam2.DataBind();
            drpTeam2.Items.Insert(0, new ListItem("Select Team", "0"));
        }
    }
    private void bindMatchType()
    {
        matchType = new MatchType(_dataObject);
        matchTypes = matchType.GetAllMatchType();
        if (matchTypes.Count > 0)
        {
            drpMatchType.DataSource = matchTypes;
            drpMatchType.DataTextField = "Type";
            drpMatchType.DataValueField = "Id";
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
    }

    private void fillMatch(string MatchId)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        matchSchedule.Id = Convert.ToInt32(MatchId);
        matchSchedule.GetMatchesById();

        if (matchSchedule.Id != 0)
        {

            drpTeam1.SelectedValue = Convert.ToString(matchSchedule.Team1);
            drpTeam2.SelectedValue = Convert.ToString(matchSchedule.Team2);
            drpMatchType.SelectedValue = Convert.ToString(matchSchedule.MatchType);
            txtDate.Text = Convert.ToString(matchSchedule.Date);
            txtTime.Text = matchSchedule.Time;
        }
    }

    //private void bindTeam()
    //{
    //    team = new Team(_dataObject);
    //    teams = team.GetAllTeam();
    //    if (teams.Count > 0)
    //    {
    //        drpTeam1.DataSource = teams;
    //        drpTeam1.DataTextField = "TeamName";
    //        drpTeam1.DataValueField = "Id";
    //        drpTeam1.DataBind();
    //        drpTeam1.Items.Insert(0, new ListItem("Select Team", "0"));

    //        drpTeam2.DataSource = teams;
    //        drpTeam2.DataTextField = "TeamName";
    //        drpTeam2.DataValueField = "Id";
    //        drpTeam2.DataBind();
    //        drpTeam2.Items.Insert(0, new ListItem("Select Team", "0"));
    //    }
    //}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        matchSchedule.Team1 = Convert.ToInt32(drpTeam1.SelectedValue);
        matchSchedule.Team2 = Convert.ToInt32(drpTeam2.SelectedValue);
        matchSchedule.MatchType = Convert.ToInt32(drpMatchType.SelectedValue);
        matchSchedule.Date = Convert.ToDateTime(txtDate.Text);
        matchSchedule.Time = txtTime.Text;
        matchSchedule.IsActive = true;
        matchSchedule.IsDeleted = false;
        bool matchExist = CheckMatchExist(Convert.ToInt32(drpTeam1.SelectedValue), Convert.ToInt32(drpTeam2.SelectedValue), Convert.ToDateTime(txtDate.Text));
        if (Convert.ToInt32(drpTeam1.SelectedValue) == Convert.ToInt32(drpTeam2.SelectedValue))
        {
            layerError.InnerHtml = "<strong>Error!</strong> You can not schedule match with same team."; ;
            layerError.Attributes.Add("style", "display:block;");
            return;
        }
        if (Request.QueryString["Id"] != null)
        {
            if (!matchExist)
            {
                matchSchedule.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                matchSchedule.DateUpdated = System.DateTime.UtcNow;
                matchSchedule.UpdateMatchSchedule();
                Response.Redirect("ViewMatches.aspx");

            }
            else
            {
                layerError.InnerHtml = "<strong>Error!</strong> Match already scheduled."; ;
                layerError.Attributes.Add("style", "display:block;");
                return;
            }
        }
        else
        {
            if (!matchExist)
            {
                matchSchedule.DateAdded = System.DateTime.UtcNow;
                matchSchedule.CreateMatchSchedule();
                layerSuccess.InnerHtml = "<strong>Success!</strong> Match added successfully.";
                layerError.Attributes.Add("style", "display:none;");
                layerSuccess.Attributes.Add("style", "display:block;");
                clearControl();
            }
            else
            {
                layerError.InnerHtml = "<strong>Error!</strong> Match already scheduled."; ;
                layerError.Attributes.Add("style", "display:block;");
                return;
            }

        }
    }

    private void clearControl()
    {
        drpTeam1.SelectedIndex = -1;
        drpTeam2.SelectedIndex = -1;
        drpMatchType.SelectedIndex = -1;
        txtDate.Text = string.Empty;
        txtTime.Text = string.Empty;

    }

    private bool CheckMatchExist(int team1, int team2, DateTime date)
    {
        bool isExist = false;
        int matchId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            matchId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        List<MatchSchedule> matchSchedules = new List<MatchSchedule>();
        matchSchedules = matchSchedule.CheckMatchExist(matchId, team1, team2, date);
        if (matchSchedules.Count > 0)
            isExist = true;
        else
            isExist = false;

        return isExist;
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewMatches.aspx");
    }
}